<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest',['except'=>['logout']]);

    }
    //用户登录
    public function login(){
        return view('home.login.login');
    }
    public function store(Request $request){
//        dump($request->toArray());die;
        $request->validate([
            'account'=>'required',
            'password'=>'required'
        ],[
            'account.required'=> '请输入用户名',
            'password.required'=>'密码不能为空',
        ]);
        if (filter_var($request->account,FILTER_VALIDATE_EMAIL)){
            $data['email']=$request->account;
        }else{
            $data['phone']=$request->account;
        }
        $data['password']=$request->password;
        if (\Auth::attempt($data,$request->remember)){
//            return redirect(route('home.index.index'));
//            session()->flash('success','登陆成功');
            return redirect ('/')->with ('success','登录成功');
        }
        return back()->with('error','账号或者密码有误');
    }
    //退出登录
    public function logout(){
       \Auth::logout();
        return redirect ('/');
    }
}
